package uk.ac.aber.paws.client.idlelogger.events;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Hashtable;

import uk.ac.aber.paws.client.constants.Constants;
import uk.ac.aber.paws.client.core.PAWSClient;
import uk.ac.aber.paws.client.idlelogger.IdleLogger;
import uk.ac.aber.paws.client.idlelogger.IdleLoggerUpdater;
import uk.ac.aber.paws.client.idlelogger.gui.PAWSSysTray;
import uk.ac.aber.paws.client.net.SendMessage;
import uk.ac.aber.paws.client.util.FileUtils;
import static uk.ac.aber.paws.client.constants.CommandConstants.*;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * ButtonHandler.java
 * 
 * Handles button presses in the Idle Logger interface
 *    
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ButtonHandler implements ActionListener {

	private int id;
	
	public ButtonHandler(int id) {
		this.id = id;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		// if the Information button is pressed
		if (id == 1) {
			PAWSSysTray.displayWindow("INFO");
		}
		
		// if the reboot button is clicked
		if (id == 2) {
			PAWSSysTray.handleShutdown();
		}
		
		// display the main window when the PAWS Client service is in a helathy state
		else if (id == 3) {
			PAWSSysTray.displayWindow("NORMAL");
		}
		
		// displays the About page 
		else if (id == 4) {
			PAWSSysTray.displayWindow("ABOUT");
		}
		
		// runs the PAWS Client Manager interface
		else if (id == 5) {
			
			String[] command;
			
			try {
				if (IdleLogger.getOS().equals(Constants.OS_FLAG_LINUX)) {
					command = COMMAND_RUN_PAWSPROGRAM_LINUX;
					command[command.length-1] = "java -jar "+IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.libdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername")+" "+
						IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.configdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagerconf")+ " >/dev/null";
					
				}
				else if (IdleLogger.getOS().equals(Constants.OS_FLAG_MAC)) {
					command = COMMAND_RUN_PAWSPROGRAM_MAC;
					command[command.length-1] = "java  -Xdock:name=\"PAWS Client Manager\" -jar "+IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.libdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername") +" "+ 
						IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.configdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagerconf")+" >/dev/null";
					
				}
				else {
					command = COMMAND_RUN_PAWSPROGRAM_WINDOWS;
					command[command.length-1] = "\""+FileUtils.convertUnixToDOSPath(IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.libdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername"))+"\" " +
						"\""+FileUtils.convertUnixToDOSPath(IdleLogger.getMainConfig().getProperty("app.main.homedir")+IdleLogger.getMainConfig().getProperty("app.main.configdir")+IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagerconf"))+"\"";
				}
				
	            ProcessBuilder pb = new ProcessBuilder(command);
	            Process p = pb.start();

			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		// runs the updates main window
		else if (id == 6) {
			
			PAWSSysTray.displayWindow("UPDATES");
		}
		
		// runs the actual update checking procedure
		else if (id == 7) {
			
			PAWSSysTray.alterUpdateButton();
			
			new IdleLoggerUpdater().start();
		
		}
		
		// fix PAWS
		else if (id == 8) {
			
			IdleLogger.performManualRestart();
		}
		
		// manual PAWS sleep
		else if (id == 9) {
			
			SendMessage.sleepMe(Integer.parseInt(IdleLogger.getMainConfig().getProperty("app.main.net.secureservercommsport")));		
		}

	}
	
	
}
